<template>
  <el-form :model="state.form" :inline="true" class="my-form" label-position="right" label-width="90px">
    <div class="my-tab">
      <el-radio-group v-model="status_label" size="large" @change="onSubmit">
        <el-radio-button v-for="(item, index) in tabList" :key="index" :label="item.val" />
      </el-radio-group>
    </div>
    <el-form-item label="订单编号">
      <el-input clearable v-model="state.form.order_no" style="width: 145px;" placeholder="请输入订单编号" />
    </el-form-item>
    <el-form-item label="活动编号">
      <el-input clearable v-model="state.form.activity_no" style="width: 145px;" placeholder="请输入活动编号" />
    </el-form-item>
    <el-form-item label="商家">
      <el-input clearable v-model="state.form.client_search" style="width: 145px;" placeholder="请输入商家名称" />
    </el-form-item>
    <el-form-item label="设备">
      <el-input clearable v-model="state.form.device_search" style="width: 145px;" placeholder="输入设备编号,名称" />
    </el-form-item>
    <el-form-item label="用户">
      <el-input clearable v-model="state.form.user_search" style="width: 145px;" placeholder="输入用户ID,邮箱" />
    </el-form-item>
    <el-form-item label="商品">
      <el-input clearable v-model="state.form.goods_search" style="width: 145px;" placeholder="输入设备商品名称" />
    </el-form-item>
    <el-form-item label="售后状态">
      <el-select clearable v-model="state.form.refund" placeholder="请选择售后状态" style="width: 145px;">
        <el-option :label="item.val" :value="item.key" v-for="item in state.refund" :key="item.key" />
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker style="width: 385px;" v-model="state.selectData" :editable="false" start-placeholder="结束时间"
        end-placeholder="结束时间" type="daterange" range-separator="至" value-format="YYYY-MM-DD" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit" :loading="isLoading" icon="Search">搜索</el-button>
      <el-button type="primary" @click="onExport" :loading="isExporting" icon="Download">导出</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, onMounted, watchEffect, ref } from 'vue'
import { getOrderEnum } from '@/api/orderManage/orderManage';
import { useCommonStore } from '@/stores';
import { isEmpty } from 'lodash-es';

const commonStore = useCommonStore();
const props = defineProps({
  isLoading: {
    type: Boolean,
    default: false
  },
  isExporting:{
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['search','export'])

const tabList = ref([{
  key: '',
  val: '全部'
}])

const status_label = ref('全部')


const state = reactive({
  form: {
    order_no: '',
    activity_no: '',
    client_search: '',
    device_search: '',
    user_search: '',
    goods_search: '',
    start_time: '',
    end_time: '',
    refund: '',
    status: ''
  },
  refund: [],
  selectData: ''
})




watchEffect(() => {
  if (state.selectData) {
    state.form.start_time = state.selectData[0]
    state.form.end_time = state.selectData[1]
  } else {
    state.form.start_time = ''
    state.form.end_time = ''
  }
  if (status_label.value) {
    state.form.status = tabList.value.find(item => item.val === status_label.value).key
  }
})

// methods
const onSubmit = () => {
  emit('search', state.form)
}
const onExport = () => {
  emit('export', state.form)
}


onMounted(async () => {
  if(!isEmpty(commonStore.orderStatusEnum)){
    state.refund = commonStore.orderStatusEnum.refund
    tabList.value = commonStore.orderStatusEnum.order
    return
  }
  const res = await getOrderEnum()
  if (res.code === 1000) {
    state.refund = res.data.refund
    tabList.value = res.data.order
    commonStore.orderStatusEnum = res.data
  }
})
</script>

<style lang="less" scoped>
.my-form {
  padding: var(--el-main-padding);
  font-size: 16px;

  .text-center {
    text-align: center;
  }

  :deep(.el-form-item) {
    margin-right: 25px;
  }

  .my-tab {
    margin-bottom: 20px;
  }
}
</style>